package com.sskj.tibi.ui.activity;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;

import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.enums.CoinEnum;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.util.NumberUtil;
import com.sskj.tibi.R;
import com.sskj.tibi.R2;
import com.sskj.tibi.bean.AssetListBean;
import com.sskj.tibi.bean.AssetTopBean;
import com.sskj.tibi.component.DaggerUserDataComponent;
import com.sskj.tibi.presenter.AssetManageActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 资产管理
 */
@Route(path = RConfig.TIBI_ASSET_MANAGE)
public class AssetManageActivity extends BaseActivity<AssetManageActivityPresenter> {
    @BindView(R2.id.tv_asset)
    TextView tvAsset;
    @BindView(R2.id.tv_asset_cny)
    TextView tvAssetCny;
    @BindView(R2.id.btn_recharge)
    Button btnRecharge;
    @BindView(R2.id.btn_withdraw)
    Button btnWithdraw;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        ImmersionBar.with(this).transparentStatusBar().statusBarDarkFont(true).init();
        return R.layout.tibi_activity_asset_manage;
    }

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    public AssetManageActivityPresenter getPresenter() {
        return new AssetManageActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.tibiassetManageActivity1));
        DaggerUserDataComponent.create().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.tibi_recy_item_asset, new SlimInjector<AssetListBean>() {
            @Override
            public void onInject(AssetListBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.item_asset_manage_type, data.getStockCode())
                        .text(R.id.cny_text, String.format(App.INSTANCE.getString(R.string.tibiassetManageActivity2), Constans.DANWEI))
                        .text(R.id.item_asset_manage_available_balance, NumberUtil.keep(data.getUsableFund(), 8, true))
                        .text(R.id.item_asset_manage_frozen_capital, NumberUtil.keep(data.getFrostFund(), 8, true))
                        .text(R.id.item_asset_manage_cny, data.getCnyPrice())
                        .clicked(R.id.item_asset_manage_record, v -> {//账户明细
                            ARouter.getInstance().build(RConfig.MINE_ASSET_RECORDS).withString("code", data.getStockCode()).navigation();
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<AssetListBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<AssetListBean>)
                page -> mPresenter.getData(page + "", "20"), 20);
        MVCCoolHelper<List<AssetListBean>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();

    }

    @Override
    protected void initData() {
        mPresenter.getTop();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    public void updateUI(AssetTopBean total) {
        tvAsset.setText(CoinUtil.keepCoinNum(CoinEnum.USDT.getCode(), total.getUsdtTotal()));
        tvAssetCny.setText("≈" + CommonUtil.dealDanwei(userData.getFound().getUsdtTotal()) + " " + Constans.DANWEI);
        ClickUtil.click(btnRecharge, () -> {
            if (!AppCircleCheckUtil.checkTradePwd(this, userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_RECHARGE).navigation();
        });

        ClickUtil.click(btnWithdraw, () -> {
            if (!AppCircleCheckUtil.checkTradePwd(this, userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_WITHDRAW).navigation();
        });
    }
}
